package com.meilai.project.mapper.business.media;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.business.intentionScheme.PointProcessSearchDTO;
import com.meilai.project.dto.business.media.MediaPointOccupyDTO;
import com.meilai.project.dto.business.media.MediaPointOccupyVO;
import com.meilai.project.dto.business.media.MediaTypeSpecsDTO;
import com.meilai.project.dto.media.AbMediaPointDTO;
import com.meilai.project.dto.media.MediaPointDTO;
import com.meilai.project.entity.business.building.BuildingArea;
import com.meilai.project.entity.media.MediaPoint;
import com.meilai.project.entity.media.MediaPointAB;
import com.meilai.project.entity.media.MediaPointExt;
import com.meilai.project.util.entity.PointRange;
import com.meilai.project.vo.app.CompanyOfflineDeviceDataVO;
import com.meilai.project.vo.business.media.*;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface MediaPointMapper extends BaseMapper<MediaPoint> {

    List<MediaPoint> selectMPForMini(@Param("query")MediaPointOccupyDTO query);

    List<MediaPointOccupyVO> selectMediaPointOccupy(@Param("start_at") LocalDate start_at, @Param("end_at") LocalDate end_at);

    @Select("SELECT " +
            " `ld`.`id` AS `id`, " +
            " `ld`.`qrcode` AS `qrcode`, " +
            " `ld`.`nfc_code` AS `nfc_code`, " +
            " `ld`.`code` AS `code`, " +
            " `ld`.`building_area_id` AS `building_area_id`, " +
            " `ld`.`building_number` AS `building_number`, " +
            " `ld`.`unit_number` AS `unit_number`, " +
            " `ld`.`floor` AS `floor`, " +
            " `ld`.`building_type` AS `building_type`, " +
            " `ld`.`media_type_id` AS `media_type_id`, " +
            " `ld`.`door_number` AS `door_number`, " +
            " `ld`.`remark` AS `remark`, " +
            " `ld`.`location` AS `location`, " +
            " `ld`.`status` AS `activate_status`, " +
            " `ld`.`import_by_code` AS `import_by_code`, " +
            " `ld`.`created_at` AS `created_at`, " +
            " `ld`.`updated_at` AS `updated_at`, " +
            " d.`name` AS `media_feature`, " +
            " `ld`.`breakdown_type` AS `breakdown_type`, " +
            " `ld`.`size_height` AS `size_length`, " +
            " `ld`.`size_width` AS `size_width`, " +
            " `ld`.`pic_type_id` AS `pic_type`,  " +
            " `ld`.`is_light` AS `is_light`  " +
            "FROM " +
            " `media_point` `ld`  " +
            "LEFT JOIN dictionary d ON ld.ab_type_id=d.id " +
            "WHERE " +
            " `ld`.`deleted_at` IS NULL")
    List<CompanyOfflineDeviceDataVO> getAppDevicesData();

    @Select("SELECT mp.`code` " +
            "FROM media_point mp " +
            "WHERE " +
            "mp.`code` regexp '${reg}' " +
            "AND mp.deleted_at IS NULL ")
    List<String> getCodeByRegexp(@Param("reg") String reg);

    @Select(" select distinct floor from media_point " +
            " where status=1 and deleted_at is null and floor is not null order by floor desc ")
    List<Integer> listFloorSelections();

    List<MediaPointAB> listAbNotOccupyABPoint(@Param("query") PointProcessSearchDTO query,
                                              @Param("points") List<MediaPointExt> mediaPoints);

    List<MediaPointExt> listByCondition(@Param("query") PointProcessSearchDTO query,
                                        @Param("pointRanges") List<PointRange> ranges,
                                        @Param("buildingList") List<BuildingArea> buildingAreaList);

    List<MediaPointExtVO> listByCondition2(@Param("query") PointProcessSearchDTO query,
                                           @Param("pointRanges") List<PointRange> ranges,
                                           @Param("buildingList") List<BuildingArea> buildingAreaList);

    List<MediaPointExt> listByBatchIds(@Param("ids") List<Long> ids);

    List<MediaPointVO> listMediaPoint(@Param("searchDTO") MediaPointDTO searchDTO);

    @Select(" select p.* from `media_point_ab` ab left join `media_point` p on ab.`b_point_id`=p.`id` and ISNULL(p.deleted_at) and p.status=1 where ab.`a_point_id`= #{a_point_id} ")
    AbMediaPointDTO getBpointByAPointId(@Param("a_point_id") Long a_point_id);

    @Select(" select p.* from `media_point_ab` ab left join `media_point` p on ab.`a_point_id`=p.`id` and ISNULL(p.deleted_at) and p.status=1 where ab.`b_point_id`= #{b_point_id} ")
    AbMediaPointDTO getApointByBPointId(@Param("b_point_id") Long b_point_id);

    @Select("select id, `code`, ab_type_id from `media_point` where building_area_id = #{building_area_id} and deleted_at is null")
    List<MediaMarketVO> getMediaMarketList(@Param("building_area_id") Long building_area_id);

    List<MediaPointAreaVO> getUnlockedPoint(@Param("page") Page<MediaPointAreaVO> page, @Param("adcode") String adcode, @Param("building_id") Long building_id, @Param("code") String code, @Param("point_type_id") Long point_type_id, @Param("ids") List<Long> ids);

    List<Long> getLockedPoint(@Param("start_time") LocalDateTime start_time, @Param("end_time") LocalDateTime end_time);

    List<MediaPointCountVO> page(@Param("page") Page<MediaPointCountVO> page,
                                 @Param("code") String code,
                                 @Param("building_area_id") Long building_area_id,
                                 @Param("media_type_id") Long media_type_id,
                                 @Param("adcode") String adcode);

    @Select("select id from `media_point` where media_type_id = #{media_type_id} and deleted_at is null")
    List<Long> getListByMediaTypeId(@Param("media_type_id") Long media_type_id);

    @Delete("delete FROM `media_point_type_specs_relation` where media_point_id = #{media_point_id} ")
    int deleteBefore(@Param("media_point_id") Long media_point_id);

    @Update("update media_point set size_height = #{dto.size_height}, size_height_inside = #{dto.size_height_inside}, size_width = #{dto.size_width}, size_width_inside = #{dto.size_width_inside}, media_type_spec_id = #{dto.media_type_specs_ids}, pic_type_id = #{dto.pic_type_id} where deleted_at is NULL and media_type_id = #{dto.media_type_id}")
    int updateByMediaTypeId(@Param("dto") MediaTypeSpecsDTO dto);

    List<Long> getMediaPointRelationIds(@Param("ids") List<Long> ids);

    List<Long> getPublishMediaPointIds(@Param("ids") List<Long> ids);
}
